/*******************************************************************************
 * Copyright (c) 2012, G. Weirich and Elexis
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    G. Weirich - initial implementation
 *******************************************************************************/

package ch.elexis.easy.developer.resources.ui;

import java.util.Arrays;
import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;

import ch.elexis.core.PersistenceException;
import ch.elexis.core.admin.InsufficientRightsException;
import ch.elexis.core.data.IActionDefinition;
import ch.elexis.easy.developer.resources.model.SampleData;
import ch.elexis.easy.ui.elements.IElexisEasyComponent;

/**
 * This creates a Component within the Elexis Easy-View. An Elexis-Easy Component consists of a Name
 * and one or more subcomponents.
 * 
 * @author gerry
 * 
 */
public class SampleComponent implements IElexisEasyComponent {
	
	private IActionDefinition[] actions = new IActionDefinition[] {
		new IActionDefinition() {
			
			@Override
			public String getText(){
				return "new SampleData";
			}
			
			@Override
			public String getToolTipText(){
				return "create a new SampleData element";
			}
			
			@Override
			public Object execute(Object params) throws InsufficientRightsException{
				SampleData s = new SampleData();
				s.set(SampleData.FLD_NAME.t(), "sampleData");
				s.set(SampleData.FLD_VALUE,100);
				return null;
			}
		}
	};
	
	@Override
	public String getLabel(){
		return "SampleData";
	}
	
	@Override
	public Image getImage(int size){
		return null;
	}
	
	/**
	 * Create a Composite that contains the named subcomponent. The Framework will place all
	 * subcomponents in a CTabFolder within the Component View. All handling of events within the
	 * subcomponents is left to the implementation.
	 */
	@Override
	public Composite createComponent(Composite parent, String subcomponent){
		Composite ret = new Composite(parent, SWT.NONE);
		ret.setLayout(new FillLayout());
		Label l = new Label(ret, SWT.NONE);
		l.setText(subcomponent);
		try {
			if (subcomponent.equals("sub1")) {
				return new Sub1(parent);
			}
		} catch (PersistenceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return ret;
	}
	
	@Override
	public IActionDefinition[] getActions(String subcomponent){
		return actions;
	}
	
}
